#!/usr/bin/python
# -*- encoding:utf-8 -*-

"""
    Linux服务器用户管理：
    一、确保组已经新增，设置sftp根目录/home/sftp
    groupadd sftp
    mkdir -p /home/sftp
    chown root:sftp /home/sftp
"""
import os


class SftpUSER(object):
    group_name: str
    sftp_home_dir: str

    def __init__(self, group_name: str, sftp_home_dir: str):
        self.group_name = group_name
        self.sftp_home_dir = sftp_home_dir

    def create_user(self, username: str, password: str):
        result: int
        result = os.system('useradd -g {} -s /sbin/nologin -d {}/{} {}'
                           .format(self.group_name, self.sftp_home_dir, username, username))
        result += os.system('chown -R {}:{} {}/{}'
                            .format(username, self.group_name, self.sftp_home_dir, username))
        result += os.system('chmod 755 {}/{}'
                            .format(self.sftp_home_dir, username))
        result += os.system('echo "{}" | passwd --stdin {}'
                            .format(password, username))
        if result == 0:  # exec success
            return True, 'add operation is successful'
        else:  # exec fail
            return False, 'add operation is failed'

    def delete_user(self, username: str):
        result: int
        result = os.system('userdel -rf {}'.format(username))
        if result == 0:  # exec success, [return a tuple]
            return True, 'del operation is successful'
        else:  # exec fail
            return False, 'del operation is failed'

